package com.example.user.customviewdemo.drawing.xfermode;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by user on 2016/5/6.
 */
public class HeartMapView extends View {
    private Bitmap dstBmp, srcBmp;
    private Paint paint, heartPaint, rectPaint;
    private Path path;
    private int rectWidth = 0;


    public HeartMapView(Context context) {
        super(context);
    }

    public HeartMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);

        heartPaint = new Paint();
        heartPaint.setAntiAlias(true);
        heartPaint.setStyle(Paint.Style.STROKE);
        heartPaint.setStrokeWidth(20);
        heartPaint.setColor(Color.GREEN);

        rectPaint = new Paint();
        rectPaint.setAntiAlias(true);
        rectPaint.setStyle(Paint.Style.FILL);
        rectPaint.setColor(Color.RED);

        path = new Path();

        dstBmp = mkDstBmp(1200, 800);

        path = new Path();
        startAnimator(1200);
    }

    public HeartMapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xff293134);

        srcBmp = mkSrcBmp(1200, 800);

        int layerID = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(dstBmp, 0, 0, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(srcBmp, 0, 0, paint);
        paint.setXfermode(null);
        canvas.restoreToCount(layerID);
    }

    /**
     * 目标图像（一个根据心脏图路径为绿色，其他地方为空白像素的Bitmap）
     *
     * @param width
     * @param height
     * @return
     */
    private Bitmap mkDstBmp(int width, int height) {
        path.reset();
        Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        path.moveTo(10, height / 2);
        path.lineTo(100, height / 2);
        path.lineTo(130, height / 2 - 100);
        path.lineTo(180, height / 2 + 100);
        path.lineTo(220, height / 2);
        path.lineTo(250, height / 2);
        path.lineTo(300, height / 2 + 200);
        path.lineTo(350, height / 2 - 200);
        path.lineTo(400, height / 2);
        path.lineTo(450, height / 2);
        c.drawPath(path,heartPaint);
        c.translate(450, 0);
        path.moveTo(0,height/2);
        path.lineTo(100, height / 2);
        path.lineTo(130, height / 2 - 100);
        path.lineTo(160, height / 2 + 100);
        path.lineTo(200, height / 2);
        path.lineTo(250, height / 2);
        path.lineTo(300, height / 2 + 200);
        path.lineTo(350, height / 2 - 200);
        path.lineTo(400, height / 2);
        path.lineTo(450, height / 2);

        c.drawPath(path, heartPaint);
        return bm;
    }

    private Bitmap mkSrcBmp(int width, int height) {
        Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        c.drawRect(0, 0, rectWidth, height, rectPaint);

        return bm;
    }

    private void startAnimator(int width) {
        ValueAnimator animator = ValueAnimator.ofInt(0, width);
        animator.setDuration(3000);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                rectWidth = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }
}


